package main

import (
	"github.com/liangdas/mqant"
	log "github.com/liangdas/mqant/log/beego"
	"github.com/liangdas/mqant/module"
	"github.com/liangdas/mqant/registry"
	"github.com/liangdas/mqant/registry/consul"
	"github.com/liangdas/mqant/rpc"
	"github.com/liangdas/mqant/rpc/pb"
	"github.com/mqant_test/helloworld"
	"github.com/mqant_test/web"
	"github.com/nats-io/nats.go"
	"time"
)

var clientRPChandler module.ClientRPCHandler = func(app module.App, server registry.Node, rpcInfo *rpcpb.RPCInfo, result interface{}, err string, execTime int64) {
	log.Info("clientRPChandler")
}

var serverRPChandler module.ServerRPCHandler = func(app module.App, module module.Module, callInfo *mqrpc.CallInfo) {
	log.Info("serverRPChandler")
}

var rpcCompleteHandler module.RpcCompleteHandler = func(app module.App, module module.Module, callInfo *mqrpc.CallInfo, input []interface{}, out []interface{}, execTime time.Duration) {
	log.Info("rpcCompleteHandler")
}


func main() {
	rs := consul.NewRegistry(func(options *registry.Options) {
		options.Addrs = []string{"127.0.0.1:8500"}
	})
	nc, err := nats.Connect("nats://127.0.0.1:4222", nats.MaxReconnects(10000))
	if err != nil {
		log.Error("nats error %v", err)
		return
	}

	app := mqant.CreateApp(
		module.Debug(true),//只有是在调试模式下才会在控制台打印日志, 非调试模式下只在日志文件中输出日志
		module.Registry(rs),
		module.Nats(nc),
		module.RegisterInterval(time.Second * time.Duration(15)),
		module.KillWaitTTL(time.Second * time.Duration(5)),
		module.RPCExpired(time.Second * time.Duration(3)),
	)

	app.UpdateOptions(
		module.SetClientRPChandler(clientRPChandler),
		module.SetServerRPCHandler(serverRPChandler),
		module.SetRpcCompleteHandler(rpcCompleteHandler),
	)

	app.OnConfigurationLoaded(func(app module.App) {
		log.Info("App OnConfigurationLoaded")
	})

	app.OnModuleInited(func(app module.App, module module.Module) {
		log.Info("App Module[%s] OnModuleInited",module.GetType())
	})

	app.OnStartup(func(app module.App) {
		log.Info("App OnStartup")
	})

	//app.SetMapRoute(func(app module.App, route string) string {
	//	log.Info("MapRoute route=%s",route)	//HelloWorld
	//	return "HelloWorld@MyHelloWorld"
	//})

	err = app.Run(
		helloworld.Module(),
		web.Module(),
	)
	if err != nil{
		log.Error(err.Error())
	}

}